<?php
/**
 * created by zzy
 * date: 2018/1/26 14:04
 */
namespace App\Eloquent\Ygt;

use App\Engine\Func;

class Logistics extends DbEloquent{

    protected $table    = 'ygt_logistics';
    public static $tableFieldList   = [
        ['name'=>'id','title'=>'','value'=>0,'is_must'=>0],
        ['name'=>'company_id','title'=>'登录用户异常','value'=>0,'is_must'=>1],
        ['name'=>'title','title'=>'快递名称','value'=>'','is_must'=>1],
        ['name'=>'logo','title'=>'快递logo','value'=>'','is_must'=>0],
        ['name'=>'first_letter','title'=>'首字母','value'=>'','is_must'=>0],
    ];
    public static function edit($data,$pk='')
    {
        $id                 = isset($data['id']) ? intval($data['id']) : 0;
        $title              = $data['title'];
        $pinyin             = app('pinyin')->abbr($title);
        $firstLetter        = strtoupper(substr($pinyin,0,1));
        $letterAll          = [];
        for($i=65;$i<91;$i++){
            $letterAll[]    = chr($i);
        }
        if(!in_Array($firstLetter,$letterAll))
        {
            $firstLetter        = '#';
        }
        $data['first_letter']   = $firstLetter;
        if($id>0){
            $where          = ['id'=>$id];
            $result         = self::updateOneData($where,$data,$pk);
        } else {
            $result         = self::insertOneData($data,$pk);
        }
        return $result;
    }
    public static function getLists($where, $limit='', $offset='')
    {
        $fileds         = 'id,title,logo,first_letter';
        $orderby        = ['id','desc'];
        $groupby        = '';
        $join           = '';
        $lists          = self::getList($where, $fileds, $limit, $offset, $orderby, $groupby, $join);
        foreach($lists as $key=>$val)
        {
            $logo       = asset('upload/appicon/logo.png');
            $imgUrl     = Func::getImgUrlById($val['logo']);
            $logo       = $imgUrl ? $imgUrl : $logo;
            $lists[$key]->logo  = $logo;
        }
        return $lists;
    }
}
